package com.mmall.service.impl;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.mmall.common.LogType;
import com.mmall.common.RequestHolder;
import com.mmall.dao.SysLogMapper;
import com.mmall.dao.SysRoleAclMapper;
import com.mmall.pojo.SysLogWithBLOBs;
import com.mmall.pojo.SysRoleAcl;
import com.mmall.service.ISysRoleAclService;
import com.mmall.util.IpUtil;
import com.mmall.util.JsonUtil;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * @Author wangtengyu
 * @Create 2018-03-16-14:17
 */
@Service("iSysRoleAclService")
public class SysRoleAclServiceImpl implements ISysRoleAclService {

    @Autowired
    private SysRoleAclMapper sysRoleAclMapper;

    @Autowired
    private SysLogMapper sysLogMapper;



    /**
     * 更改权限
     * @param roleId
     * @param aclIdList
     */
    public void changeAcls(int roleId, List<Integer> aclIdList){
        //首先判断下权限是不是没有任何变化，如果没有任何变化就不在数据库中查了

        //获取老的权限id集合
        List<Integer> oldAclIdList=sysRoleAclMapper.getAclIdListByRoleId(Lists.newArrayList(roleId));
        //如果没改变 就不走数据库了
        if(oldAclIdList.size()==aclIdList.size()){//长度相同才判断集合内元素是否相同
            //新的权限id集合 aclIdList
            //生成set 无序 唯一
            Set<Integer> oldSet= Sets.newHashSet(oldAclIdList);
            Set<Integer> newSet=Sets.newHashSet(aclIdList);
            oldSet.remove(newSet);
            if(CollectionUtils.isEmpty(oldSet)){
                return;
            }
        }

        updateRoleAcls(roleId,aclIdList);
        saveRoleAclLog(roleId,oldAclIdList,aclIdList);

    }

    @Transactional
    public void updateRoleAcls(int roleId, List<Integer> aclIdList) {
        if (CollectionUtils.isEmpty(aclIdList)){
            sysRoleAclMapper.deleteByRoleId(roleId);
            return;
        }

        List<SysRoleAcl> roleAclList=Lists.newArrayList();
        for(Integer aclId:aclIdList){
            SysRoleAcl roleAcl=SysRoleAcl.builder().roleId(roleId).aclId(aclId).operator(RequestHolder.getCurrentUser().getUsername())
                    .operateIp(IpUtil.getRemoteIp(RequestHolder.getCurrentRequest())).operateTime(new Date()).build();
            roleAclList.add(roleAcl);
        }
        sysRoleAclMapper.deleteByRoleId(roleId);
        sysRoleAclMapper.batchInsert(roleAclList);

    }

    private void saveRoleAclLog(int roleId, List<Integer> before, List<Integer> after) {
        SysLogWithBLOBs sysLog = new SysLogWithBLOBs();
        sysLog.setType(LogType.TYPE_ROLE_ACL);
        sysLog.setTargetId(roleId);
        sysLog.setOldValue(before == null ? "" : JsonUtil.obj2String(before));
        sysLog.setNewValue(after == null ? "" : JsonUtil.obj2String(after));
        sysLog.setOperator(RequestHolder.getCurrentUser().getUsername());
        sysLog.setOperateIp(IpUtil.getRemoteIp(RequestHolder.getCurrentRequest()));
        sysLog.setOperateTime(new Date());
        sysLog.setStatus(1);
        sysLogMapper.insertSelective(sysLog);
    }
}
